package com.oa.ylms.apps.actions.account;

import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.TextAnchor;

import com.boco.base.communication.accessclient.IAccessClientEvent;
import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.account.IAccountInfoService;
import com.oa.ylms.apps.business.archives.IStaffInfoService;
import com.oa.ylms.apps.constants.account.IAccountInfoStates;
import com.oa.ylms.apps.entity.account.AccountInfoValue;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;

@SuppressWarnings("serial")
public class AccountInfoAction extends BasicAction {
	private IAccountInfoService accountService;
	private List<AccountInfoValue> accountList;
	private List<AccountInfoValue> inList;
	private List<AccountInfoValue> outList;
	private AccountInfoValue accountInfo;
	private IStaffInfoService staffService;
	private boolean isSuccess;
	private Integer inOrOut=0;
	private Integer accountId;
	private Integer submitState;
	private String comments;
	private StaffInfoValue staffInfo;
	private Integer states=0;
	private String imageUrl;
	private String imageUrl1;
	private String searchYear;
	private String searchMonth;
	private Double inTotal = 0.0;
	private Double outTotal = 0.0;
	
	

	/**
	 * 1.入账管理 2.出账管理 3.财务分析
	 */
	public String execute() {
		try {
			staffInfo = (StaffInfoValue)session.get("login_staffInfo");
			accountList = accountService.queryAccountInfoWithPage(staffInfo,
					inOrOut,states, rows, currpage);
			totalpage=accountService.countTPages(staffInfo, inOrOut,states, rows);
			count=accountService.getTotalNumber();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public String add() {
		try {
			staffInfo=(StaffInfoValue)session.get("login_staffInfo");
			accountService.addAccountInfo(staffInfo,accountInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "add";
	}
	public String toManagerList(){
		try {
			accountList=accountService.queryAccountInfoWithPage(staffInfo, inOrOut, states, rows, currpage);
			totalpage=accountService.countTPages(staffInfo, inOrOut,states, rows);
			count=accountService.getTotalNumber();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toManagerList";
	}
	

	public String toCheckList() {
		try {
			staffInfo = (StaffInfoValue)session.get("login_staffInfo");
			accountList = accountService
					.queryAccountCheck(staffInfo,inOrOut);
			totalpage=accountService.countTPages(staffInfo, inOrOut, states, rows);
			count=accountService.getTotalNumber();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toCheckList";
	}

	public String check() {
		try {
			staffInfo = (StaffInfoValue)session.get("login_staffInfo");
			if (submitState == 1) {
				accountService.checkPass(staffInfo, accountId, new String(comments
						.getBytes("ISO-8859-1"), "UTF-8"));
			}
			if (submitState == 0) {
				accountService.checkUnpass(staffInfo, accountId, new String(comments
						.getBytes("ISO-8859-1"), "UTF-8"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "check";
	}
	
	public String queryByManager() throws Exception{
		Integer year = null;
		Integer month = null;
		if(searchYear != null)
			year = Integer.parseInt(searchYear);
		if(!"".equals(searchMonth) && searchMonth != null)
			month = Integer.parseInt(searchMonth);
		if(inOrOut == null || inOrOut == 0)
			inOrOut = null;
		if(searchYear != null){
			accountList = accountService.queryAccountList(year, month, inOrOut);
			accountService.countTotal(accountList);
			inTotal = accountService.getInTotalInfo();
			outTotal = accountService.getOutTotalInfo();
		}
		return "queryByManager";
	}
	
	public String queryByInOrOut() throws Exception{
		Integer year = null;
		Integer month = null;
		if(searchYear != null)
			year = Integer.parseInt(searchYear);
		if(!"".equals(searchMonth) && searchMonth != null)
			month = Integer.parseInt(searchMonth);
		if(searchYear != null){
			inList = accountService.queryAccountList(year, month, IAccountInfoStates.ACCOUNTIN);
			accountService.countTotal(inList);
			inTotal = accountService.getInTotalInfo();
			outList = accountService.queryAccountList(year, month, IAccountInfoStates.ACCOUNTOUT);
			accountService.countTotal(outList);
			outTotal = accountService.getOutTotalInfo();
		}
		return "queryByInOrOut";
	}
	
	public String query() throws Exception{
		if(searchYear != null && "".equals(searchMonth)){
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		
		for(Integer i =1; i < 13; i++){
			accountService.queryAccountInfo(Integer.parseInt(searchYear), i, null);
			dataset.addValue(accountService.getInTotalInfo(), "收入", i);
			dataset.addValue(accountService.getOutTotalInfo(), "支出", i);
		}
		
		JFreeChart chart = ChartFactory.createBarChart3D(searchYear+"年度财务分析", "月份", "金额(单位:元)", dataset, PlotOrientation.VERTICAL, true, false, false);
		
		CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
		BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer();
		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//显示每个柱的数值 
		renderer.setBaseItemLabelsVisible(true); 
		//注意：此句很关键，若无此句，那数字的显示会被覆盖，给人数字没有显示出来的问题 
		renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition( 
		ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); 
		renderer.setItemLabelAnchorOffset(2D);// 设置柱形图上的文字偏离值 

		plot.setRenderer(renderer);

		CategoryAxis domainAxis=plot.getDomainAxis();
		//水平底部列表
		domainAxis.setLabelFont(new Font("黑体",Font.BOLD,14));
		//水平底部标题
		domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,12));
		//垂直标题
	    ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
	    rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,15));

	    chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
	    chart.getTitle().setFont(new Font("黑体", Font.BOLD, 20));
		
		ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
		String filename = ServletUtilities.saveChartAsPNG(chart, 500, 370, info, httpSession);
		imageUrl = request.getContextPath()+"/servlet/DisplayChart?filename="+filename;
		}
		///////////////////////////////
		if(searchYear != null && !"".equals(searchMonth)){
		DefaultPieDataset dataset1 = new DefaultPieDataset();
		
		accountService.queryAccountInfo(Integer.parseInt(searchYear), Integer.parseInt(searchMonth), null);
		dataset1.setValue("收入(单位:元)", accountService.getInTotalInfo());
		dataset1.setValue("支出(单位:元)", accountService.getOutTotalInfo());
		
		JFreeChart chart1 = ChartFactory.createPieChart3D(searchYear+"年度"+searchMonth+"月份财务分析", dataset1, true, false, false);

		PiePlot3D plot1 = (PiePlot3D) chart1.getPlot();
		// 图片中显示百分比:默认方式
		plot1.setLabelGenerator(new StandardPieSectionLabelGenerator(
				StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
		// 图片中显示百分比:自定义方式，{0} 表示选项， {1} 表示数值，
		//{2} 表示所占比例 ,小数点后两位
		plot1.setLabelGenerator(new StandardPieSectionLabelGenerator(
				"{0}={1}({2})", NumberFormat.getNumberInstance(),
				new DecimalFormat("0.00%")));
		// 图例显示百分比:自定义方式， {0} 表示选项， {1} 表示数值， {2} 表示所占比例
		plot1.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
						"{0}"));
		// 指定图片的透明度(0.0-1.0)
		plot1.setForegroundAlpha(1.0f);
		// 指定显示的饼图上圆形(true)还椭圆形(false)
		plot1.setCircular(true);
		// 设置图上分类标签label的字体，解决中文乱码
		plot1.setLabelFont(new Font("黑体", Font.PLAIN, 12));
		// 设置图上分类标签label的最大宽度，相对与plot的百分比
		plot1.setMaximumLabelWidth(0.2);
		// 设置3D饼图的Z轴高度（0.0～1.0）
		plot1.setDepthFactor(0.07);
		//设置起始角度，默认值为90，当为0时，起始值在3点钟方向
		plot1.setStartAngle(45);
 
		// 设置图标题的字体，解决中文乱码
		TextTitle textTitle = chart1.getTitle();
		textTitle.setFont(new Font("黑体", Font.PLAIN, 20));

		// 设置Legend部分字体，解决中文乱码
		chart1.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
		
		ChartRenderingInfo info1 = new ChartRenderingInfo(new StandardEntityCollection());
		String filename1 = ServletUtilities.saveChartAsPNG(chart1, 500, 370, info1, httpSession);
		imageUrl1 = request.getContextPath()+"/servlet/DisplayChart?filename="+filename1;
		}
		return "query";
	}

	public String queryCheck() {
		return "queryCheck";
	}

	public String queryCheckByInOrOut() {
		return "queryCheckByInOrOut";
	}

	public String queryCheckByTime() {
		return "queryCheckByTime";
	}
	public IAccountInfoService getAccountService() {
		return accountService;
	}
	public void setAccountService(IAccountInfoService accountService) {
		this.accountService = accountService;
	}
	public List<AccountInfoValue> getAccountList() {
		return accountList;
	}
	public void setAccountList(List<AccountInfoValue> accountList) {
		this.accountList = accountList;
	}
	public AccountInfoValue getAccountInfo() {
		return accountInfo;
	}
	public void setAccountInfo(AccountInfoValue accountInfo) {
		this.accountInfo = accountInfo;
	}
	public IStaffInfoService getStaffService() {
		return staffService;
	}
	public void setStaffService(IStaffInfoService staffService) {
		this.staffService = staffService;
	}
	public boolean isSuccess() {
		return isSuccess;
	}
	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}
	public Integer getInOrOut() {
		return inOrOut;
	}
	public void setInOrOut(Integer inOrOut) {
		this.inOrOut = inOrOut;
	}
	public Integer getAccountId() {
		return accountId;
	}
	public void setAccountId(Integer accountId) {
		this.accountId = accountId;
	}
	public Integer getSubmitState() {
		return submitState;
	}
	public void setSubmitState(Integer submitState) {
		this.submitState = submitState;
	}
	public String getComments() {
		return comments;
	}
	public void setComments(String comments) {
		this.comments = comments;
	}
	public Integer getStates() {
		return states;
	}
	public void setStates(Integer states) {
		this.states = states;
	}
	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}
	public String getImageUrl() {
		return imageUrl;
	}
	public void setImageUrl1(String imageUrl1) {
		this.imageUrl1 = imageUrl1;
	}
	public String getImageUrl1() {
		return imageUrl1;
	}
	public void setSearchYear(String searchYear) {
		this.searchYear = searchYear;
	}
	public String getSearchYear() {
		return searchYear;
	}
	public void setSearchMonth(String searchMonth) {
		this.searchMonth = searchMonth;
	}
	public String getSearchMonth() {
		return searchMonth;
	}
	public void setInTotal(Double inTotal) {
		this.inTotal = inTotal;
	}
	public Double getInTotal() {
		return inTotal;
	}
	public void setOutTotal(Double outTotal) {
		this.outTotal = outTotal;
	}
	public Double getOutTotal() {
		return outTotal;
	}
	public void setInList(List<AccountInfoValue> inList) {
		this.inList = inList;
	}
	public List<AccountInfoValue> getInList() {
		return inList;
	}
	public void setOutList(List<AccountInfoValue> outList) {
		this.outList = outList;
	}
	public List<AccountInfoValue> getOutList() {
		return outList;
	}



}
